﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sunimage.Spjg.BLL.SysMgr;
using Sunimage.Spjg.Common.SysMgr;
using Sunimage.Spjg.Web.Helper;
using Sunimage.Spjg.Web.UserControls;
using Sunimage.Spjg.Common.Exceptions;
using Sunimage.Spjg.Common;
using System.Data.SqlClient;

namespace Sunimage.Spjg.Web.Admin.Sys
{
    public partial class Users : System.Web.UI.Page
    {
        UserBl userBl = new UserBl();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindUsers();
            }
        }

        protected void tvGsj_SelectedNodeChanged(object sender, EventArgs e)
        {
            BindUsers();
            //tbxGsjNameAdd.Text = tvGsj.SelectedNode.Text;
        }

        private void BindUsers()
        {
            gvUsers.DataSource = userBl.GetUsers();
            gvUsers.DataBind();
        }

        protected void Pager_PageChanged(object sender, EventArgs e)
        {
            gvUsers.PageIndex = pager.CurrentPageIndex - 1;
            BindUsers();
        }

        //搜索
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            //ViewState["username"] = tbxUsernameSrch.Text.Trim();
            //ViewState["realname"] = tbxNameSrch.Text.Trim();
            //BindUsers();
        }

        protected void gvUsers_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //点击编辑
            if (e.CommandName == "EditUser")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                Response.Redirect("UsersEdit.aspx?id=" + id);

            }
            //点击删除
            else if (e.CommandName == "DeleteUser")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                try
                {
                    userBl.Delete(id);
                    BindUsers();
                    MessageHelper.RegShowJSAjax(gvUsers, "删除用户成功", this.Page);
                }
                catch (ReferDataNotExistException)
                {
                    AlertHelper.ShowError("由于存在用户数据，此用户不允许删除！", this.Page);
                }
                catch (BusinessObjectException excp)
                {
                    AlertHelper.ShowError(excp.Message, this.Page);
                }
            }
            //点击重置密码
            else if (e.CommandName == "ResetPwd")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                userBl.ResetPassword(id);
                //BindUsers();
                MessageHelper.RegShowJSAjax(gvUsers, "重置用户密码成功", this.Page);
            }
        }

        protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewHelper.AddRowNumber(e);
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label ltr = e.Row.FindControl("lblRoleName") as Label;
                User user = e.Row.DataItem as User;
                if (user.Role == RoleType.Administrator)
                {
                    ltr.Text = "管理员";
                }
                else
                {
                    ltr.Text = "普通用户";
                }
            }
        }


    }
}